<?php


namespace app\index\controller;


use think\Db;

class Matter extends \Common {

    # 获取全部事件
    public function GetAllCases1(){
        $caseOne = Db::name("case_one")->where("is_del", 0)->select();
        $data = [];
        if(!empty($caseOne)){
            foreach ($caseOne as $value){
                $caseTwo = Db::name("case_two")->where("co_id", $value["co_id"])->where("is_del", 0)->select();
                $children = [];
                foreach ($caseTwo as $val){
                    $children[] = [
                        "value"       => $val["ct_id"],
                        "label"     => $val["name"],
                    ];
                }
                if(!empty($children)){
                    $data[] = [
                        "value"       => $value["co_id"],
                        "label"     => $value["name"],
                        "children" => $children
                    ];
                }
            }
        }

        $this->Code(200, false, $data);
    }

    # 获取全部事件
    public function GetAllCases(){
        $caseOne = Db::name("case_one")->where("is_del", 0)->select();
        $data = [];
        if(!empty($caseOne)){
            foreach ($caseOne as $value){
                $caseTwo = Db::name("case_two")->where("co_id", $value["co_id"])->where("is_del", 0)->select();
                $children = [];
                foreach ($caseTwo as $val){
                    $children[] = [
                        "id"       => $val["ct_id"],
                        "name"     => $val["name"],
                        "parentId" => $val["co_id"]
                    ];
                }
                $data[] = [
                    "id"       => $value["co_id"],
                    "name"     => $value["name"],
                    "children" => $children
                ];
            }
        }

        $this->Code(200, false, $data);
    }

    # 更新 / 添加投诉信息
    public function UpdateCaseParent(){
        if(isset($_POST["name"])){
            $info = [
                "name" => $_POST["name"]
            ];
            if(isset($_POST["id"])){
                $check = Db::name("case_one")->where("co_id != {$_POST["id"]}")->where("name", $_POST["name"])->where("is_del", 0)->find();
                if(empty($check)){
                    Db::name("case_one")->where("co_id", $_POST["id"])->update($info);
                    $this->Code(200, "更新成功！", (Object)[]);
                }else{
                    $this->Code(403, "重复名称！");
                }
            }else{
                $check = Db::name("case_one")->where("name", $_POST["name"])->where("is_del", 0)->find();
                if(empty($check)){
                    Db::name("case_one")->insert($info);
                    $this->Code(200, "添加成功！", (Object)[]);
                }else{
                    $this->Code(403, "重复添加！");
                }
            }
        }elsE{
            $this->Code(403, 403);
        }
    }

    # 更新 / 添加投诉详情
    public function UpdateCaseChild(){
        if(isset($_POST["name"]) && isset($_POST["parentId"])){
            $info = [
                "name"  => $_POST["name"],
                "co_id" => $_POST["parentId"]
            ];
            if(isset($_POST["id"])){
                $check = Db::name("case_two")->where("ct_id != {$_POST["id"]}")->where("co_id", $_POST["parentId"])->where("name", $_POST["name"])->where("is_del", 0)->find();
                if(empty($check)){
                    Db::name("case_two")->where("ct_id", $_POST["id"])->update($info);
                    $mess = Db::name("case_two")->where("ct_id", $_POST["id"])->find();
                    $data = [
                        "id"    => (int)$mess["ct_id"],
                        "parentId" => (int)$mess["co_id"],
                        "name"  => $mess["name"]
                    ];
                    $this->Code(200, "更新成功！", $data);
                }else{
                    $this->Code(403, "重复名称！");
                }
            }else{
                $check = Db::name("case_two")->where("co_id", $_POST["parentId"])->where("name", $_POST["name"])->where("is_del", 0)->find();
                if(empty($check)){
                    $ct_id = Db::name("case_two")->insertGetId($info);
                    $mess = Db::name("case_two")->where("ct_id", $ct_id)->find();
                    $data = [
                        "id"    => (int)$mess["ct_id"],
                        "co_id" => (int)$mess["co_id"],
                        "name"  => $mess["name"]
                    ];
                    $this->Code(200, "添加成功！", $data);
                }else{
                    $this->Code(403, "重复添加！");
                }
            }
        }else{
            $this->Code(403, 403);
        }
    }

    # 删除投诉原因
    public function DelCaseParent(){
        if(isset($_POST["id"])){
            $info = [
                "is_del" => 1
            ];
            $have = Db::name("case_two")->where("co_id", $_POST["id"])->where("is_del", 0)->count();
            if($have > 0){
                $this->Code(403, "分类下有内容，无法删除！");
            }else{
                Db::name("case_one")->where("co_id", $_POST["id"])->update($info);
                $this->Code(200, "删除成功！");
            }
        }else{
            $this->Code(403, 403);
        }
    }

    # 删除投诉详情
    public function DelCaseChild(){
        if(isset($_POST["id"])){
            $info = [
                "is_del" => 1
            ];
            Db::name("case_two")->where("ct_id", $_POST["id"])->update($info);
            $this->Code(200, "删除成功！");
        }else{
            $this->Code(403, 403);
        }
    }

    public function DelMatter(){
        if(isset($_POST["id"])){
            Db::name("import")->where("iid", $_POST["id"])->delete();
            $this->Code(200, "删除成功");
        }
    }

    public function GetMatterList(){
        $aid = $this->token["aid"];
        if(isset($_GET["status"]) && isset($_GET["user_id"]) && isset($_GET["ct_id"])){
            $where = []; $user_id = $aid; $ct_id = $_GET["ct_id"]; $page = $_GET["page"]; $limit = $_GET["limit"];

            $ct_id != "" && $where["ct_id"] = $ct_id;
            if(isset($_GET["self"]) && $_GET["self"] == 1){
                $where["aid"] = $aid;
            }
            if($_GET["status"] != 0){
                if($_GET["status"] == 4){
                    $where["state"] = [1, 2, 3];
                    if($aid != 1){
                        $where["pid"] = $aid;
                    }
                }else{
                    $where["state"] = $_GET["status"];
                }
                if($user_id != "" && $user_id != 1){
                    $where["pid"] = $user_id;
                }else if($_GET["user_id"] != ""){
                    $where["pid"] = $_GET["user_id"];
                }
            }else{
                $_GET["user_id"] != "" && $where["pid"] = $_GET["user_id"];
            }
            $time = strtotime("-3 month"); // 获取3个月之前的时间戳
            $where2 = "state<>3 or (state=3 and startTime>".$time.")"; // 获取未完成的或者已完成，但时间未超过3个月的
            $import = Db::name("import")->where($where)->where($where2)->order("urgency desc, endTime asc, state asc")->page($page, $limit)->select();
            $total = Db::name("import")->where($where)->where($where2)->count();
            $list = [];

            if(isset($_GET['related']) && $_GET['related'] == 1){
                $import = Db::name("import")->where($where)->where($where2)->order("urgency desc, endTime asc, state asc")->select();
                $total = Db::name("import")->where($where)->where($where2)->count();
                $list = [];
                foreach ($import as $value){
                    $related_id = explode(",", $value['related_id']);
                    if ($value['related_id'] != "" && in_array($aid, $related_id)) {
                        $admin   = Db::name("admin")->where("aid", $value["pid"])->find();
                        $admin1   = Db::name("admin")->where("aid", $value["aid"])->find();
                        $caseTwo = Db::name("case_two")->where("ct_id", $value["ct_id"])->find();
                        if(!empty($caseTwo)){
                            $case_id = [(int)$caseTwo["co_id"], (int)$value["ct_id"]];
                            $case_name = $caseTwo["name"];
                        }else{
                            $case_id = [0, (int)$value["ct_id"]];
                            $case_name = '';
                        }
                        $arr = explode(",", $value["related_id"]);
                        foreach ($arr as $key => $val){
                            $arr[$key] = (int)$val;
                        }
                        $list[] = [
                            "id"          => (int)$value["iid"],
                            "user_id"     => (int)$value["pid"] == 0 ? '' : (int)$value["pid"],
                            "user"        => empty($admin) ? "" : $admin["name"],
                            "author"      => empty($admin1) ? "" : $admin1["name"],
                            // "case_id"     => [(int)$caseTwo["co_id"], (int)$value["ct_id"]],
                            "case_id"     => $case_id,
                            // "case"        => $caseTwo["name"],
                            "case"        => $case_name,
                            "content"     => $value["content"],
                            "reply"       => $value["remark"],
                            "urgency"     => $value["urgency"] == 1,
                            "timeRange"   => [(int)$value["startTime"] * 1000, (int)$value["endTime"] * 1000],
                            "status"      => (int)$value["state"],
                            "isSelf"      => $aid == $value["aid"],
                            "isPrincipal" => $aid == $value["pid"],
                            "related_ids" => $arr
                        ];
                    }else{
                        $total--;
                    }
                }

                $list = array_slice($list,($page - 1) * $limit,$limit);

            }else{
                foreach ($import as $value){
                    $admin   = Db::name("admin")->where("aid", $value["pid"])->find();
                    $admin1   = Db::name("admin")->where("aid", $value["aid"])->find();
                    $caseTwo = Db::name("case_two")->where("ct_id", $value["ct_id"])->find();
                    if(!empty($caseTwo)){
                        $case_id = [(int)$caseTwo["co_id"], (int)$value["ct_id"]];
                        $case_name = $caseTwo["name"];
                    }else{
                        $case_id = [0, (int)$value["ct_id"]];
                        $case_name = '';
                    }
                    $arr = explode(",", $value["related_id"]);
                    foreach ($arr as $key => $val){
                        $arr[$key] = (int)$val;
                    }
                    $list[] = [
                        "id"          => (int)$value["iid"],
                        "user_id"     => (int)$value["pid"] == 0 ? '' : (int)$value["pid"],
                        "user"        => empty($admin) ? "" : $admin["name"],
                        "author"      => empty($admin1) ? "" : $admin1["name"],
                        // "case_id"     => [(int)$caseTwo["co_id"], (int)$value["ct_id"]],
                        "case_id"     => $case_id,
                        // "case"        => $caseTwo["name"],
                        "case"        => $case_name,
                        "content"     => $value["content"],
                        "reply"       => $value["remark"],
                        "urgency"     => $value["urgency"] == 1,
                        "timeRange"   => [(int)$value["startTime"] * 1000, (int)$value["endTime"] * 1000],
                        "status"      => (int)$value["state"],
                        "isSelf"      => $aid == $value["aid"],
                        "isPrincipal" => $aid == $value["pid"],
                        "related_ids" => $arr
                    ];
                }
            }

            $data = [
                "list"  => $list,
                "total" => $total
            ];

            $this->Code(200, false, $data);
        }else{
            $this->Code(403, 403);
        }
    }

    public function updateMatter(){
        $aid = $this->token["aid"];
        if(isset($_POST["id"])){ # 修改
            if(!isset($_POST["timeRange"])){
                $upInfo = [
                    "urgency"   => $_POST["urgency"] == "false" ? 0 : 1,
                ];
                Db::name("import")->where("iid", $_POST["id"])->update($upInfo);
                $this->Code(200, "修改成功");
            }else{
                $time = explode(",", $_POST["timeRange"]);
                $start = (int)$time[0] / 1000; $end = (int)$time[1] / 1000; $ct_id = 0;
                $_POST["case_id"] != "" && $case = explode(",", $_POST["case_id"]); $ct_id = $case[1];
                $upInfo = [
                    "startTime" => $start,
                    "endTime"   => $end,
                    "remark"    => $_POST["reply"],
                    "urgency"   => $_POST["urgency"] == "false" ? 0 : 1,
                    "content"   => $_POST["content"],
                    "ct_id"     => $ct_id,
                    "pid"       => $_POST["user_id"] == "" ? 0 : $_POST["user_id"],
                    "related_id"   => $_POST["related_ids"]
                ];
                Db::name("import")->where("iid", $_POST["id"])->update($upInfo);
                $this->Code(200, "修改成功");
            }
        }else{ # 添加
            $time = explode(",", $_POST["timeRange"]);
            $start = (int)$time[0] / 1000; $end = (int)$time[1] / 1000; $ct_id = 0;
            $_POST["case_id"] != "" && $case = explode(",", $_POST["case_id"]); $ct_id = $case[1];
            $addInfo = [
                "aid"       => $aid,
                "startTime" => $start,
                "endTime"   => $end,
                "remark"    => $_POST["reply"],
                "urgency"   => $_POST["urgency"] == "false" ? 0 : 1,
                "content"   => $_POST["content"],
                "ct_id"     => $ct_id,
                "pid"       => $_POST["user_id"] == "" ? 0 : $_POST["user_id"],
                "related_id"   => $_POST["related_ids"]
            ];
            Db::name("import")->insert($addInfo);
            $this->Code(200, "添加成功");
        }
    }


    public function SetMatterState(){
        if(isset($_POST["id"]) && isset($_POST["status"])){
            $upInfo = [
                "state" => $_POST["status"]
            ];
            Db::name("import")->where("iid", $_POST["id"])->update($upInfo);
            $this->Code(200, "修改成功");
        }else{
            $this->Code(403, 403);
        }
    }

}
